
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "BrowseHTMList.h"
#include "MainFrm.h"

//#include "BrowseHTMListView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWndEx)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWndEx)
	ON_WM_CREATE()
	ON_COMMAND(ID_VIEW_CUSTOMIZE, &CMainFrame::OnViewCustomize)
	ON_REGISTERED_MESSAGE(AFX_WM_CREATETOOLBAR, &CMainFrame::OnToolbarCreateNew)
	ON_COMMAND_RANGE(ID_VIEW_APPLOOK_WIN_2000, ID_VIEW_APPLOOK_WINDOWS_7, &CMainFrame::OnApplicationLook)
	ON_UPDATE_COMMAND_UI_RANGE(ID_VIEW_APPLOOK_WIN_2000, ID_VIEW_APPLOOK_WINDOWS_7, &CMainFrame::OnUpdateApplicationLook)
	ON_COMMAND(ID_PREVIOUS, &CMainFrame::OnPrevious)
	ON_COMMAND(ID_NEXT, &CMainFrame::OnNext)
	ON_UPDATE_COMMAND_UI(ID_PREVIOUS, &CMainFrame::OnUpdatePrevious)
	ON_UPDATE_COMMAND_UI(ID_NEXT, &CMainFrame::OnUpdateNext)
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_PROGRESS, &CMainFrame::OnUpdateIndicatorProgress)
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_BAD_ITEM, &CMainFrame::OnUpdateIndicatorBadItem)
	ON_COMMAND(ID_SYNCBYBEEP, &CMainFrame::OnSyncbybeep)
	ON_UPDATE_COMMAND_UI(ID_ITEM_MARKBAD, &CMainFrame::OnUpdateItemMarkbad)
	ON_COMMAND(ID_ITEM_SKIP, &CMainFrame::OnItemSkip)
	ON_COMMAND(ID_ITEM_MARKBAD, &CMainFrame::OnItemMarkbad)
	ON_COMMAND(ID_ITEM_RETRY, &CMainFrame::OnItemRetry)
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_BAD_ITEM,
	ID_INDICATOR_PROGRESS,
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	theApp.m_nAppLook = theApp.GetInt(_T("ApplicationLook"), ID_VIEW_APPLOOK_VS_2008);
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWndEx::OnCreate(lpCreateStruct) == -1)
		return -1;

	BOOL bNameValid;

	if (!m_wndMenuBar.Create(this))
	{
		TRACE0("Failed to create menubar\n");
		return -1;      // fail to create
	}

	m_wndMenuBar.SetPaneStyle(m_wndMenuBar.GetPaneStyle() | CBRS_SIZE_DYNAMIC | CBRS_TOOLTIPS | CBRS_FLYBY);

	// prevent the menu bar from taking the focus on activation
	CMFCPopupMenu::SetForceMenuFocus(FALSE);

	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(theApp.m_bHiColorIcons ? IDR_MAINFRAME_256 : IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	CString strToolBarName;
	bNameValid = strToolBarName.LoadString(IDS_TOOLBAR_STANDARD);
	ASSERT(bNameValid);
	m_wndToolBar.SetWindowText(strToolBarName);

	CString strCustomize;
	bNameValid = strCustomize.LoadString(IDS_TOOLBAR_CUSTOMIZE);
	ASSERT(bNameValid);
	m_wndToolBar.EnableCustomizeButton(TRUE, ID_VIEW_CUSTOMIZE, strCustomize);

	if (!m_wndStatusBar.Create(this))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}
	m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT));

	// TODO: Delete these five lines if you don't want the toolbar and menubar to be dockable
	m_wndMenuBar.EnableDocking(CBRS_ALIGN_ANY);
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockPane(&m_wndMenuBar);
	DockPane(&m_wndToolBar);


	// enable Visual Studio 2005 style docking window behavior
	CDockingManager::SetDockingMode(DT_SMART);
	// enable Visual Studio 2005 style docking window auto-hide behavior
	EnableAutoHidePanes(CBRS_ALIGN_ANY);
	// set the visual manager and style based on persisted value
	OnApplicationLook(theApp.m_nAppLook);

	// Enable toolbar and docking window menu replacement
	EnablePaneMenu(TRUE, ID_VIEW_CUSTOMIZE, strCustomize, ID_VIEW_TOOLBAR);

	// enable quick (Alt+drag) toolbar customization
	CMFCToolBar::EnableQuickCustomization();

	m_hAccel = ::LoadAccelerators(AfxGetInstanceHandle(),
                              MAKEINTRESOURCE(IDR_MAINFRAME));
	ASSERT(m_hAccel);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWndEx::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWndEx::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWndEx::Dump(dc);
}
#endif //_DEBUG


// CMainFrame message handlers

void CMainFrame::OnViewCustomize()
{
	CMFCToolBarsCustomizeDialog* pDlgCust = new CMFCToolBarsCustomizeDialog(this, TRUE /* scan menus */);
	pDlgCust->Create();
}

LRESULT CMainFrame::OnToolbarCreateNew(WPARAM wp,LPARAM lp)
{
	LRESULT lres = CFrameWndEx::OnToolbarCreateNew(wp,lp);
	if (lres == 0)
	{
		return 0;
	}

	CMFCToolBar* pUserToolbar = (CMFCToolBar*)lres;
	ASSERT_VALID(pUserToolbar);

	BOOL bNameValid;
	CString strCustomize;
	bNameValid = strCustomize.LoadString(IDS_TOOLBAR_CUSTOMIZE);
	ASSERT(bNameValid);

	pUserToolbar->EnableCustomizeButton(TRUE, ID_VIEW_CUSTOMIZE, strCustomize);
	return lres;
}

void CMainFrame::OnApplicationLook(UINT id)
{
	CWaitCursor wait;

	theApp.m_nAppLook = id;

	switch (theApp.m_nAppLook)
	{
	case ID_VIEW_APPLOOK_WIN_2000:
		CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManager));
		break;

	case ID_VIEW_APPLOOK_OFF_XP:
		CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerOfficeXP));
		break;

	case ID_VIEW_APPLOOK_WIN_XP:
		CMFCVisualManagerWindows::m_b3DTabsXPTheme = TRUE;
		CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));
		break;

	case ID_VIEW_APPLOOK_OFF_2003:
		CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerOffice2003));
		CDockingManager::SetDockingMode(DT_SMART);
		break;

	case ID_VIEW_APPLOOK_VS_2005:
		CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerVS2005));
		CDockingManager::SetDockingMode(DT_SMART);
		break;

	case ID_VIEW_APPLOOK_VS_2008:
		CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerVS2008));
		CDockingManager::SetDockingMode(DT_SMART);
		break;

	case ID_VIEW_APPLOOK_WINDOWS_7:
		CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows7));
		CDockingManager::SetDockingMode(DT_SMART);
		break;

	default:
		switch (theApp.m_nAppLook)
		{
		case ID_VIEW_APPLOOK_OFF_2007_BLUE:
			CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_LunaBlue);
			break;

		case ID_VIEW_APPLOOK_OFF_2007_BLACK:
			CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_ObsidianBlack);
			break;

		case ID_VIEW_APPLOOK_OFF_2007_SILVER:
			CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_Silver);
			break;

		case ID_VIEW_APPLOOK_OFF_2007_AQUA:
			CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_Aqua);
			break;
		}

		CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerOffice2007));
		CDockingManager::SetDockingMode(DT_SMART);
	}

	RedrawWindow(NULL, NULL, RDW_ALLCHILDREN | RDW_INVALIDATE | RDW_UPDATENOW | RDW_FRAME | RDW_ERASE);

	theApp.WriteInt(_T("ApplicationLook"), theApp.m_nAppLook);
}

void CMainFrame::OnUpdateApplicationLook(CCmdUI* pCmdUI)
{
	pCmdUI->SetRadio(theApp.m_nAppLook == pCmdUI->m_nID);
	//::MessageBeep(0);
}


bool CMainFrame::HTMLItemChange(void)
{
	theApp.m_bBadItem = false;
	theApp.LoadHTMLItem();
	return true;
}


void CMainFrame::OnPrevious()
{
	// TODO: Add your command handler code here
	theApp.logHTMLItem(_T(""));
	HTMITEM item;
	item = theApp.m_HTMList.GetPrev();
	HTMLItemChange();
//	AfxMessageBox(item.item_id);
}


void CMainFrame::OnNext()
{
	// TODO: Add your command handler code here
	
	theApp.logHTMLItem(_T(""));
	HTMITEM item;
	item = theApp.m_HTMList.GetNext();
	HTMLItemChange();
//	AfxMessageBox(item.item_id);
}


void CMainFrame::OnSyncbybeep()
{
	// TODO: Add your command handler code here
	theApp.logBeep();
	/*
	::Sleep(2000);
	INPUT    Input={0};													// Create our input.

	Input.type        = INPUT_MOUSE;									// Let input know we are using the mouse.
	Input.mi.dwFlags  = MOUSEEVENTF_LEFTDOWN;							// We are setting left mouse button down.
	SendInput( 1, &Input, sizeof(INPUT) );								// Send the input.

	ZeroMemory(&Input,sizeof(INPUT));									// Fills a block of memory with zeros.
	Input.type        = INPUT_MOUSE;									// Let input know we are using the mouse.
	Input.mi.dwFlags  = MOUSEEVENTF_LEFTUP;								// We are setting left mouse button up.
	SendInput( 1, &Input, sizeof(INPUT) );								// Send the input.
	*/
}


void CMainFrame::OnUpdatePrevious(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	if (theApp.m_HTMList.IsFirst())
		pCmdUI->Enable(0);
	else
		pCmdUI->Enable(1);
}


void CMainFrame::OnUpdateNext(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	if (theApp.m_HTMList.IsLast())
		pCmdUI->Enable(0);
	else
		pCmdUI->Enable(1);
}

void CMainFrame::OnUpdateIndicatorProgress(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	if (theApp.m_HTMList.IsEmpty())
	{
		pCmdUI->Enable(0);
		pCmdUI->SetText(_T("Load a list"));
	}
	else
	{
		pCmdUI->Enable(1);
		CString str;
		str.Format( _T("%d of %d"), theApp.m_HTMList.GetCurrentIndex(), theApp.m_HTMList.GetCount() ); 
		pCmdUI->SetText(str);
	}
}

void CMainFrame::OnUpdateIndicatorBadItem(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	if (theApp.m_HTMList.IsEmpty())
	{
		pCmdUI->Enable(0);
		pCmdUI->SetText(_T("No Items"));
	}
	else
	{
		pCmdUI->Enable(1);
		if (theApp.m_bBadItem)
			pCmdUI->SetText(_T("Bad item"));
		else
			pCmdUI->SetText(_T("Good item"));
	}
}


void CMainFrame::OnUpdateItemMarkbad(CCmdUI *pCmdUI)
{
	// TODO: Add your command update UI handler code here
	if (theApp.m_HTMList.IsEmpty())
		return;
	if (theApp.m_bBadItem)
		pCmdUI->SetCheck(1);
	else
		pCmdUI->SetCheck(0);

}


BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class
	if(m_hAccel)
	{
		if(::TranslateAccelerator(m_hWnd, m_hAccel, pMsg))
		   return(TRUE);
	}

	return CFrameWndEx::PreTranslateMessage(pMsg);
}



void CMainFrame::OnItemMarkbad()
{
	// TODO: Add your command handler code here
	if (theApp.m_HTMList.IsEmpty())
		return;
	theApp.m_bBadItem = !theApp.m_bBadItem;
}

void CMainFrame::OnItemSkip()
{
	// TODO: Add your command handler code here
	if (theApp.m_HTMList.IsEmpty())
		return;
	theApp.m_bBadItem = true;
	PostMessage(WM_COMMAND, ID_NEXT, 0);
}


void CMainFrame::OnItemRetry()
{
	// TODO: Add your command handler code here
	if (theApp.m_HTMList.IsEmpty())
		return;
	theApp.logHTMLItem(_T(""));
	HTMLItemChange();
}
